﻿using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Threading;
using Artech.PetShop.Common;
namespace Artech.PetShop.Infrastructures
{
    public class ContextSendInspector: IClientMessageInspector
    {
        #region IClientMessageInspector Members

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {            
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            if (string.IsNullOrEmpty(ApplicationContext.Current.UserName))
            {
                ApplicationContext.Current.UserName = Thread.CurrentPrincipal.Identity.Name;
            }
            request.Headers.Add(new MessageHeader<ApplicationContext>(
                ApplicationContext.Current).GetUntypedHeader(
                ApplicationContext.ContextHeaderLocalName, ApplicationContext.ContextHeaderNamespace));

            return null;
        }

        #endregion
    }
}